package com.yuan.weike.controller;

import com.yuan.weike.common.BaseResponse;
import com.yuan.weike.common.ResultUtils;
import com.yuan.weike.model.User;
import com.yuan.weike.model.request.*;
import com.yuan.weike.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     *
     * @param userRegisterRequest 用户注册请求参数
     * @return 用户ID
     */
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        long result = userService.userRegister(userRegisterRequest);
        return ResultUtils.success(result);
    }

    /**
     * 添加用户（仅管理员可用）
     *
     * @param userAddRequest 添加用户请求参数
     * @param request        HTTP请求
     * @return 用户ID
     */
    @PostMapping("/add")
    public BaseResponse<Long> addUser(@RequestBody UserAddRequest userAddRequest, HttpServletRequest request) {
        User currentUser = (User) request.getSession().getAttribute("loginUser");
        if (currentUser == null) {
            return (BaseResponse<Long>) ResultUtils.error(401, "用户未登录");
        }
        long result = userService.addUser(userAddRequest, currentUser);
        return ResultUtils.success(result);
    }

    /**
     * 用户登录
     *
     * @param userLoginRequest 用户登录请求参数
     * @param request          HTTP请求
     * @return 登录用户信息
     */
    @PostMapping("/login")
    public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        User user = userService.userLogin(userLoginRequest);
        request.getSession().setAttribute("loginUser", user);
        return ResultUtils.success(user);
    }

    /**
     * 用户注销
     *
     * @param request HTTP请求
     * @return 是否注销成功
     */
    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        request.getSession().removeAttribute("loginUser");
        return ResultUtils.success(true);
    }

    /**
     * 获取当前登录用户
     *
     * @param request HTTP请求
     * @return 当前登录用户信息
     */
    @GetMapping("/current")
    public BaseResponse<User> getCurrentUser(HttpServletRequest request) {
        User currentUser = (User) request.getSession().getAttribute("loginUser");
        if (currentUser == null) {
            return (BaseResponse<User>) ResultUtils.error(401, "用户未登录");
        }
        User user = userService.getCurrentUser(currentUser.getUserId());
        return ResultUtils.success(user);
    }

    /**
     * 更新用户信息
     *
     * @param userUpdateRequest 用户更新信息请求参数
     * @param request           HTTP请求
     * @return 是否更新成功
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateUserInfo(@RequestBody UserUpdateRequest userUpdateRequest, HttpServletRequest request) {
        User currentUser = (User) request.getSession().getAttribute("loginUser");
        if (currentUser == null) {
            return (BaseResponse<Boolean>) ResultUtils.error(401, "用户未登录");
        }
        boolean result = userService.updateUserInfo(userUpdateRequest, currentUser);
        return ResultUtils.success(result);
    }

    /**
     * 找回密码
     *
     * @param resetPasswordRequest 找回密码请求参数
     * @return 是否重置成功
     */
    @PostMapping("/reset-password")
    public BaseResponse<Boolean> resetPassword(@RequestBody UserResetPasswordRequest resetPasswordRequest) {
        boolean result = userService.resetPassword(resetPasswordRequest);
        return ResultUtils.success(result);
    }

    /**
     * 获取用户列表（仅管理员可用）
     *
     * @param request HTTP请求
     * @return 用户列表
     */
    @GetMapping("/list")
    public BaseResponse<List<User>> listUsers(HttpServletRequest request) {
        User currentUser = (User) request.getSession().getAttribute("loginUser");
        if (currentUser == null) {
            return (BaseResponse<List<User>>) ResultUtils.error(401, "用户未登录");
        }
        List<User> userList = userService.listUsers(currentUser);
        return ResultUtils.success(userList);
    }

    /**
     * 更新用户状态（仅管理员可用）
     *
     * @param userId  用户ID
     * @param status  新状态
     * @param request HTTP请求
     * @return 是否更新成功
     */
    @PostMapping("/update/status")
    public BaseResponse<Boolean> updateUserStatus(@RequestParam long userId, @RequestParam int status, HttpServletRequest request) {
        User currentUser = (User) request.getSession().getAttribute("loginUser");
        if (currentUser == null) {
            return (BaseResponse<Boolean>) ResultUtils.error(401, "用户未登录");
        }
        boolean result = userService.updateUserStatus(userId, status, currentUser);
        return ResultUtils.success(result);
    }

    /**
     * 删除用户（仅管理员可用）
     *
     * @param userId  用户ID
     * @param request HTTP请求
     * @return 是否删除成功
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteUser(@RequestParam long userId, HttpServletRequest request) {
        User currentUser = (User) request.getSession().getAttribute("loginUser");
        if (currentUser == null) {
            return (BaseResponse<Boolean>) ResultUtils.error(401, "用户未登录");
        }
        boolean result = userService.deleteUser(userId, currentUser);
        return ResultUtils.success(result);
    }
}